#include "adminleaveemployeeview.h"

AdminLeaveEmployeeView::AdminLeaveEmployeeView(QWidget *parent) : QWidget(parent)
{
    adminManager = new AdminManager(this);
    setupUI();
    setupConnections();
}

AdminLeaveEmployeeView::~AdminLeaveEmployeeView()
{
    delete adminManager;
}

void AdminLeaveEmployeeView::setupUI()
{
    // 设置窗口标题
    setWindowTitle("员工离职");

    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // 创建标题标签
    QLabel *titleLabel = new QLabel("员工离职");
    QFont titleFont("Microsoft YaHei", 14, QFont::Bold);
    titleLabel->setFont(titleFont);
    titleLabel->setAlignment(Qt::AlignCenter);
    mainLayout->addWidget(titleLabel);

    // 创建表单布局
    QVBoxLayout *formLayout = new QVBoxLayout();

    // 员工ID
    QHBoxLayout *idLayout = new QHBoxLayout();
    QLabel *idLabel = new QLabel("员工ID:");
    empIdEdit = new QLineEdit();
    empIdEdit->setPlaceholderText("请输入员工ID");
    idLayout->addWidget(idLabel);
    idLayout->addWidget(empIdEdit);
    formLayout->addLayout(idLayout);

    // 按钮布局
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    leaveButton = new QPushButton("确认离职");
    resetButton = new QPushButton("重置");
    buttonLayout->addWidget(leaveButton);
    buttonLayout->addWidget(resetButton);
    formLayout->addLayout(buttonLayout);

    mainLayout->addLayout(formLayout);
}

void AdminLeaveEmployeeView::setupConnections()
{
    connect(leaveButton, &QPushButton::clicked, this, &AdminLeaveEmployeeView::onLeaveButtonClicked);
    connect(resetButton, &QPushButton::clicked, this, &AdminLeaveEmployeeView::onResetButtonClicked);
}

void AdminLeaveEmployeeView::onLeaveButtonClicked()
{
    QString empId = empIdEdit->text().trimmed();

    // 输入验证
    if (empId.isEmpty())
    {
        QMessageBox::warning(this, "输入错误", "请输入员工ID");
        return;
    }

    // 确认离职
    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(this, "确认离职", QString("确定要让员工 %1 离职吗？").arg(empId),
                                  QMessageBox::Yes | QMessageBox::No);

    if (reply == QMessageBox::Yes)
    {
        // 调用AdminManager的离职方法
        bool success = adminManager->removeEmployee(empId);
        if (success)
        {
            QMessageBox::information(this, "操作成功", "员工已成功离职");
            onResetButtonClicked();
        }
        else
        {
            QMessageBox::critical(this, "操作失败", "员工离职失败，请检查员工ID是否正确");
        }
    }
}

void AdminLeaveEmployeeView::onResetButtonClicked()
{
    empIdEdit->clear();
}
